package com.my.aslife.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.aslife.mapper.CommentMapper;
import com.my.aslife.module.common.ErrorCode;
import com.my.aslife.module.common.ResponseResult;
import com.my.aslife.module.common.UserIdThreadLocal;
import com.my.aslife.module.entity.Comment;
import com.my.aslife.module.entity.Orders;
import com.my.aslife.module.entity.RComment;
import com.my.aslife.module.entity.UComment;
import com.my.aslife.module.entity.dtos.CommentDto;
import com.my.aslife.module.entity.dtos.CommentGetDto;
import com.my.aslife.service.CommentService;
import com.my.aslife.service.OrdersService;
import com.my.aslife.service.RCommentService;
import com.my.aslife.service.UCommentService;
import com.my.aslife.utils.CheckSensitiveUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author mayuan
 * @description TODO
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private UCommentService uCommentService;

    @Autowired
    private RCommentService rCommentService;

    @Override
    public ResponseResult pushComment(CommentDto commentDto) {

        ResponseResult responseResult = ordersService.queryOrderByOrderId(commentDto.getOrderId());
        Orders orders = (Orders) responseResult.getData();

        LambdaQueryWrapper<Comment> lqwComment = new LambdaQueryWrapper<>();
        lqwComment.eq(Comment::getOrderId, commentDto.getOrderId());
        Comment commentQuery = getOne(lqwComment);
        if (commentQuery == null) {
            Comment comment = new Comment();
            comment.setOrderId(commentDto.getOrderId());
            comment.setUserId(orders.getUserId());
            comment.setRunnerId(orders.getRunnerId());
            save(comment);
        }

        Map<String, Integer> stringIntegerMap = CheckSensitiveUtil.matchWords(commentDto.getComment());
        if (stringIntegerMap.size() != 0) {
            return ResponseResult.failed(ErrorCode.ESTIMATE_SENSITIVE);
        }

        // 0 用户 1 配送者
        if (commentDto.getType() == 0) {
            UComment uComment = new UComment();
            uComment.setOrderId(commentDto.getOrderId());
            uComment.setComment(commentDto.getComment());
            uComment.setUserId(Long.valueOf(UserIdThreadLocal.get()));
            uComment.setCreateTime(new Date());
            uCommentService.save(uComment);
        } else {
            RComment rComment = new RComment();
            rComment.setOrderId(commentDto.getOrderId());
            rComment.setComment(commentDto.getComment());
            rComment.setRunnerId(Long.valueOf(UserIdThreadLocal.get()));
            rComment.setCreateTime(new Date());
            rCommentService.save(rComment);
        }
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult getComment(Long orderId) {
        LambdaQueryWrapper<UComment> lqwUComment = new LambdaQueryWrapper<>();
        lqwUComment.eq(UComment::getOrderId, orderId);
        UComment ucomment = uCommentService.getOne(lqwUComment);

        LambdaQueryWrapper<RComment> lqwRComment = new LambdaQueryWrapper<>();
        lqwRComment.eq(RComment::getOrderId, orderId);
        RComment rComment = rCommentService.getOne(lqwRComment);

        CommentGetDto commentGetDto = new CommentGetDto();
        if (rComment != null) {
            commentGetDto.setRComment(rComment.getComment());
        }
        if (ucomment != null) {
            commentGetDto.setUComment(ucomment.getComment());
        }

        return ResponseResult.ok(commentGetDto);
    }
}
